package nuc.ss.wl.controller;

import nuc.ss.wl.entity.SC;
import nuc.ss.wl.entity.S_SC_C;
import nuc.ss.wl.service.SCService;
import nuc.ss.wl.service.impl.SCServiceImpl;
import nuc.ss.wl.utils.WebUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


public class SCServlet extends BaseServlet {


    protected void findS_SC_S(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SCService service = new SCServiceImpl();
        int cno = WebUtil.parseInt(request.getParameter("cno"), 0);
        List<S_SC_C> s_sc_cList = service.findAll(cno);
        request.setAttribute("s_sc_cList", s_sc_cList);
        if (s_sc_cList != null) {
            request.getRequestDispatcher("/pages/manager/student_choose_course.jsp").forward(request, response);
        } else {

        }
    }

    protected void findSC(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int sno = Integer.parseInt(request.getParameter("sno"));
        int cno = Integer.parseInt(request.getParameter("cno"));
        System.out.println(sno+cno);
        SCService service = new SCServiceImpl();
        SC bySnoAndCno = service.findBySnoAndCno(sno, cno);
        request.setAttribute("sc", bySnoAndCno);
        if (bySnoAndCno != null) {
            request.getRequestDispatcher("/pages/manager/sc_edit.jsp").forward(request, response);
        } else {

        }
    }


    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int sno = Integer.parseInt(request.getParameter("sno"));
        int cno = Integer.parseInt(request.getParameter("cno"));
        float grade = Float.parseFloat(request.getParameter("grade"));
        SCService service = new SCServiceImpl();
        service.update(new SC(sno, cno, grade));
        response.sendRedirect(request.getContextPath() + "/scServlet?action=findS_SC_S&cno=" + cno);
    }

}
